package com.cn;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

/**
 * 2月15号
 * 测试
 */
@SpringBootTest
public class FlowableDemo02 {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private HistoryService historyService;



    /**
     * 任务通过
     */
    @Test
    void test01(){
        String id = "3098c551-cba3-11ee-a8e6-30d04231d1ff";
        HashMap<String, Object> map = new HashMap<>();
        map.put("num",4);
        taskService.complete(id,map);
    }

    /**
     *启动
     */
    @Test
    void processInstanceTest() {
        identityService.setAuthenticatedUserId("1");
        HashMap<String, Object> map = new HashMap<>();
        map.put("proposer","zhanwang");
        runtimeService.startProcessInstanceByKey("approval",map);
    }

    /**
     * 查询代办
     */
    @Test
    void name() {
        String taskId = "1";
        List<Task> list = taskService.createTaskQuery()
                .taskCandidateUser(taskId)
                .list();
        if (!list.isEmpty()) {
            List<Map<String,Object>> taskList= new ArrayList<>();
            for (Task task : list) {
                Map<String,Object> stringMap = new HashMap<>();
                String id = task.getId();
                String name = task.getName();
                String description = task.getDescription();
                stringMap.put("id",id);
                stringMap.put("name",name);
                stringMap.put("description",description);
                taskList.add(stringMap);
            }
            System.out.println(taskList+"---");
        }
    }

    /**
     * 开始审批
     */
    @Test
    void name02() {
        String taskId = "87f9fe0d-cbb9-11ee-84b5-30d04231d1ff";
        HashMap<String, Object> map = new HashMap<>();
        map.put("num",6);
        taskService.complete(taskId,map);
    }

    /**
     * 查询已经完成的任务
     */
    @Test
    void name03() {
        //获取当前
        String userId = "1";
        List<HistoricTaskInstance> taskInstanceQuery = historyService.createHistoricTaskInstanceQuery()
                .includeProcessVariables()
                .finished()
                .taskAssignee(userId)
                .orderByHistoricTaskInstanceEndTime()
                .desc()
                .list();

        System.out.println(taskInstanceQuery+"测试");

    }
}
